var app = getApp();
const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 年月日时分
const formatTime1 = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 年月日
const formatTime2 = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('-')
}

// 月日
const mdTime = now => {
    let dates = new Date(now)
    var month = dates.getMonth() + 1;
    var date = dates.getDate();
    return [month, date].map(formatNumber).join('-')
}

// 日期转时间戳
const toTimeStamp = (dateString) => {
    if (!dateString) return '';
    const systemInfo = wx.getSystemInfoSync();
    if (systemInfo.platform === "ios") {
        if (dateString.indexOf('-') !== -1) {
            dateString = dateString.replaceAll('-', '/');
        }
    } else {
        if (dateString.indexOf('/') !== -1) {
            dateString = dateString.replaceAll('/', '-');
        }
    }
    return new Date(dateString).getTime();
}


const week = (now, flag = 1) => {
    let days = null;
    if (flag == 1) {
        let date = new Date(now);
        days = days
    } else {
        days = now;
    }
    var week;
    if (days == 0) week = "星期日"
    if (days == 1) week = "星期一"
    if (days == 2) week = "星期二"
    if (days == 3) week = "星期三"
    if (days == 4) week = "星期四"
    if (days == 5) week = "星期五"
    if (days == 6) week = "星期六"
    return week;
}

const shareEvent = (option, obj = null) => {
    let shareObj = {
        title: obj ? obj.title : '摩尔小程序',
        path: obj ? obj.path : '/pages/index/index',
        imageUrl: obj ? obj.imageUrl : '/images/shares.jpg',
        // 转发成功之后的回调
        success(res) {
            if (res.errMsg == 'shareAppMessage:ok') {}
        },
        // 转发失败之后的回调
        fail(res) {
            //用户取消转发
            if (res.errMsg == 'shareAppMessage:fail cancel') {
                // 转发失败，其中 detail message 为详细失败信息
            } else if (res.errMsg == 'shareAppMessage:fail') {

            }
        },
        // 转发结束之后的回调（转发成不成功都会执行）
        complete() {}
    };
    if (option.from === 'button') { // 来自页面内转发按钮
        console.log(option.target)
    }
    return shareObj;
}

const regularConfig = {
    IDcard: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/, // 身份证
    mobile: /^1([1|2|3|4|5|7|8|9|])\d{9}$/,
    telephone: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/,
    num: /^[0-9]*$/, // 数字
    phoneNo: /(^1([3|4|5|7|8|])\d{9}$)|(^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$)/, // 电话或者手机
    isEmail:/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,//邮箱正则
    policeNo: /^[0-9A-Za-z]{4,10}$/, // 账号4-10位数字或字母组成
    pwd: /^[0-9A-Za-z]{6,16}$/, // 密码由6-16位数字或者字母组成
    isNumAlpha: /^[0-9A-Za-z]*$/, // 字母或数字
    isAlpha: /^[a-zA-Z]*$/, // 是否字母
    isNumAlphaCn: /^[0-9a-zA-Z\u4E00-\uFA29]*$/, // 是否数字或字母或汉字
    isPostCode: /^[\d\-]*$/i, // 是否邮编
    isNumAlphaUline: /^[0-9a-zA-Z_]*$/, // 是否数字、字母或下划线
    isNumAndThanZero: /^([1-9]\d*(\.\d+)?|0)$/, // 是否为整数且大于0/^[1-9]\d*(\.\d+)?$/
    isNormalEncode: /^(\w||[\u4e00-\u9fa5]){0,}$/, // 是否为非特殊字符（包括数字字母下划线中文）
    isTableName: /^[a-zA-Z][A-Za-z0-9\#\$\_\-]{0,29}$/, // 表名
    isInt: /^-?\d+$/, // 整数
    isTableOtherName: /^[\u4e00-\u9fa5]{0,20}$/, // 别名
    imgType: /image\/(png|jpg|jpeg|gif)$/, // 上传图片类型
    isChina: /^[\u4e00-\u9fa5]{2,8}$/,
    isNozeroNumber: /^\+?[1-9]\d*$/, // 大于零的正整数
    float: /^\d+(\.?|(\.\d+)?)$/, // 匹配正整数或者小数 或者0.这个特殊值
    isBoolean: function (d) {
        if (typeof d == 'boolean') {
            return true;
        }
        return false;
    },
    isObject: function (d) {
        if (typeof d == 'object') {
            return true;
        }
        return false;
    },
    isFunction: function (d) {
        if (typeof d == 'function') {
            return true;
        }
        return false;
    },
    isString: function (d) {
        if (d && d != 'null' && d != null && d != 'undefined' && d != '' && d != ' ' && typeof d == 'string') {
            return true;
        }
        return false;
    },
    isNumber: function (d) {
        if (typeof d == 'number') {
            return true;
        }
        return false;
    }
};


const getUserInfo = (userInfo, isLogin = false) => {
    let users = {
        is_login: false,
        is_submit: false
    };
    if (userInfo && regularConfig.isObject(userInfo)) {
        userInfo.is_login = userInfo.mobile && regularConfig.isString(userInfo.mobile) ? true : false;
        userInfo.is_submit = userInfo.case_no && regularConfig.isString(userInfo.case_no) ? true : false;
        userInfo.coupon_num =userInfo.coupon_num?userInfo.coupon_num:0;
        users = userInfo;
    }
    if (isLogin) {
        if (!users.is_login) {
            wx.showModal({
                title: '登录提示',
                content: '此操作需要您登录小程序，是否立即登录？',
                complete: (res) => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/login/login',
                        })
                    }
                }
            });
            return false;
        }
        if (!users.is_submit) {
            wx.showModal({
                title: '绑定就诊卡',
                content: '此操作需要您绑定就诊卡，是否立即绑定？',
                complete: (res) => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/mine/thePatient/thePatient',
                        })
                    }
                }
            });
            return false;
        }
    }
    return users;
}

module.exports = {
    formatTime,
    formatTime1,
    formatTime2,
    mdTime,
    week,
    shareEvent,
    regularConfig,
    getUserInfo,
    toTimeStamp
}